﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using K.DLInterface;
using K.EnterpriseLibary.Entity;
using System.Data.OleDb;
namespace K.DLBySelf
{
    public class ChineseDictDao : DbBaseForAccess, IChineseDict
    {
        private int GetNewId()
        {
            string sql = "select curr_val + incrementby from bn_sequence";
            object o = DbHelper.ExecuteScalar(CommandType.Text, sql, null);
            string sqlu = "update bn_sequence set curr_val = " + o.ToString() + " where SEQ_name = 'DICTIONARY_SEQ' ";
            DbHelper.ExecuteNonQuery(CommandType.Text, sqlu, null);
            return Convert.ToInt32(o);
             
        }
        private int GetCharacterId(string font)
        {
            string sql = "select id from bn_character where font = '" + font + "'";
            object o = DbHelper.ExecuteScalar(CommandType.Text, sql,null);
            return Convert.ToInt32(o);
        }

        private int GetCharacterInfoId(string meaning,string soundMark,string partOfSpeech)
        {
            string sql = "select id from bn_character_info where meaning = '" + meaning + "' and soundmark = '" + soundMark + "' and part_of_speech = '" + partOfSpeech + "'";
            object o = DbHelper.ExecuteScalar(CommandType.Text, sql,null);
            return Convert.ToInt32(o);
        }

        private int GetCharacterInfoProvenanceId(int characterInfoId, string content, string articleName, string authorName)
        {
            string sql = "select id form bn_character_info_provenance where character_info_id = " + characterInfoId.ToString() + " and article_name = '" + articleName + "' and author_name = '" + authorName + "'";
            object o = DbHelper.ExecuteScalar(CommandType.Text, sql);
            return Convert.ToInt32(o);
        }

        public bool InsertCharacterInfoProvenance(EnterpriseLibary.Entity.CharacterInfoProvenance characterInfoProvenance)
        {
            string sql = "insert into bn_character_info_provenance(id,character_info_id,content,article_name,aurth_name) values ('" + GetNewId().ToString() + "','" + GetCharacterInfoId(characterInfoProvenance.CharacterInfo.Meaning,characterInfoProvenance.CharacterInfo.SoundMark,characterInfoProvenance.CharacterInfo.PartOfSpeech).ToString() + "','" + characterInfoProvenance.Content + "','" + characterInfoProvenance.ArticelName + "','" + characterInfoProvenance.AurthName + "')";
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
            return true;
        }

        public bool UpdateCharacterInfoProvenancer(EnterpriseLibary.Entity.CharacterInfoProvenance characterInfoProvenance)
        {
            throw new NotImplementedException();
        }

        public bool DeleteCharacterInfoProvenance(EnterpriseLibary.Entity.CharacterInfoProvenance characterInfoProvenance)
        {
            string sql = "delete from bn_character_info_provenance where id = " + GetCharacterInfoProvenanceId(GetCharacterInfoId(characterInfoProvenance.CharacterInfo.Meaning,characterInfoProvenance.CharacterInfo.SoundMark,characterInfoProvenance.CharacterInfo.PartOfSpeech), characterInfoProvenance.Content, characterInfoProvenance.ArticelName, characterInfoProvenance.AurthName).ToString() + "";
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
            return true;
        }

        public EnterpriseLibary.Entity.CharacterInfoProvenance SelectCharacterInfoProvenanceById(int id)
        {
            throw new NotImplementedException();
        }

        public List<CharacterInfoProvenance> SelectCharacterInfoProvenanceByAnyProperty(params object[] parameters)
        {
            throw new NotImplementedException();
        }

        public bool InsertArticle(EnterpriseLibary.Entity.Article article)
        {
            throw new NotImplementedException();
        }

        public bool UpdateArticle(EnterpriseLibary.Entity.Article article)
        {
            throw new NotImplementedException();
        }

        public bool DeleteArticle(EnterpriseLibary.Entity.Article article)
        {
            throw new NotImplementedException();
        }

        public EnterpriseLibary.Entity.Article SelectArticleById(int id)
        {
            throw new NotImplementedException();
        }

        public List<EnterpriseLibary.Entity.Article> SelectArticleByAnyProperty(params object[] parameters)
        {
            throw new NotImplementedException();
        }

        public bool InsertBook(EnterpriseLibary.Entity.Book book)
        {
            throw new NotImplementedException();
        }

        public bool UpdateBook(EnterpriseLibary.Entity.Book book)
        {
            throw new NotImplementedException();
        }

        public bool DeleteBook(EnterpriseLibary.Entity.Book book)
        {
            throw new NotImplementedException();
        }

        public EnterpriseLibary.Entity.Book SelectBookById(int id)
        {
            throw new NotImplementedException();
        }

        public List<EnterpriseLibary.Entity.Book> SelectBookByAnyProperty(params object[] parameters)
        {
            throw new NotImplementedException();
        }

        public bool InsertCharacter(EnterpriseLibary.Entity.Character character)
        {
            throw new NotImplementedException();
        }

        public bool UpdateCharacter(EnterpriseLibary.Entity.Character character)
        {
            string sql = "update bn_character set radicals = '" + character.Radicals + "',stroke_count = '" + character.StrokeCount + "',complete_flag = '" + character.CompleteFlag + "' where font = '" + character.Font + "'";
            if (DbHelper.ExecuteNonQuery(CommandType.Text, sql, null) == 0)
                return false;
            return true;
        }

        public bool DeleteCharacter(EnterpriseLibary.Entity.Character character)
        {
            throw new NotImplementedException();
        }

        public EnterpriseLibary.Entity.Character SelectCharacterById(int id)
        {
            throw new NotImplementedException();
        }

        public List<EnterpriseLibary.Entity.Character> SelectCharacterByAnyProperty(params object[] parameters)
        {
            throw new NotImplementedException();
        }

        public bool InsertCharacterInfo(EnterpriseLibary.Entity.CharacterInfo characterInfo)
        {
            string sql = "insert into bn_character_Info (id,character_id,meaning,soundmark,part_of_speech) values (" + GetNewId().ToString() + "," + GetCharacterId(characterInfo.Character.Font).ToString() + ",'" + characterInfo.Meaning + "','" + characterInfo.SoundMark + "','" + characterInfo.PartOfSpeech + "')";
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
            return true;
        }

        public bool UpdateCharacterInfo(EnterpriseLibary.Entity.CharacterInfo characterInfo)
        {
            throw new NotImplementedException();
        }

        public bool DeleteCharacterInfo(EnterpriseLibary.Entity.CharacterInfo characterInfo)
        {
            string sql = "delete from bn_character_info where id = " + GetCharacterInfoId(characterInfo.Meaning, characterInfo.SoundMark, characterInfo.PartOfSpeech).ToString();
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
            return true;
        }

        public EnterpriseLibary.Entity.CharacterInfo SelectCharacterInforById(int id)
        {
            throw new NotImplementedException();
        }


        
        
        public List<EnterpriseLibary.Entity.CharacterInfo> SelectCharacterInfoByAnyProperty(params object[] parameters)
        {
            throw new NotImplementedException();
            //string sql = "select * from bn_character_info where character_id = "+  +""
        }

        public bool InsertSoundMark(EnterpriseLibary.Entity.SoundMark soundMark)
        {
            throw new NotImplementedException();
        }

        public bool UpdateSoundMark(EnterpriseLibary.Entity.SoundMark soundMark)
        {
            throw new NotImplementedException();
        }

        public bool DeleteSoundMark(EnterpriseLibary.Entity.SoundMark soundMark)
        {
            throw new NotImplementedException();
        }

        public EnterpriseLibary.Entity.SoundMark SelectSoundMarkById(int id)
        {
            throw new NotImplementedException();
        }

        public List<EnterpriseLibary.Entity.SoundMark> SelectSoundMarkByAnyProperty(params object[] parameters)
        {
            throw new NotImplementedException();
        }


        public EnterpriseLibary.Entity.Character SelectRandomCharacter()
        {
            Character character = new Character();
            string sql = "SELECT TOP 1 *  FROM BN_CHARACTER where complete_flag is NULL and is_common = 0 order by id";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, null);
            if (dr.Read())
            {
                character.Id = Convert.ToInt32(dr["ID"]);
                character.Font = dr["FONT"].ToString();
                character.Radicals = dr["RADICALS"].ToString();
                character.StrokeCount = Convert.ToInt32(dr["STROKE_COUNT"]);
            }
            dr.Close();
            return character;
        }


        public List<CharacterInfo> SelectCharacterInfoByCharacter(Character character)
        {
            List<CharacterInfo> listCharacterInfo = new List<CharacterInfo>();
            string sql = "select * from bn_character_info where character_id = " + GetCharacterId(character.Font).ToString();
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, null);
            while (dr.Read())
            {
                CharacterInfo characterInfo = new CharacterInfo();
                characterInfo.Character = character;
                characterInfo.Meaning = dr["MEANING"].ToString();
                characterInfo.SoundMark = dr["SOUNDMARK"].ToString();
                characterInfo.PartOfSpeech = dr["PART_OF_SPEECH"].ToString();
                listCharacterInfo.Add(characterInfo);
            }
            dr.Close();
            return listCharacterInfo;
        }




        public List<CharacterInfoProvenance> SelectCharacterInfoProvenanceByCharacterInfo(CharacterInfo characterInfo)
        {
            List<CharacterInfoProvenance> listCharacterInfoProvenance = new List<CharacterInfoProvenance>();
            string sql = "select * from bn_character_info_provenance where character_info_id = " + GetCharacterInfoId(characterInfo.Meaning, characterInfo.SoundMark, characterInfo.PartOfSpeech).ToString();
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, null);
            while (dr.Read())
            {
                CharacterInfoProvenance characterInfoProvenance = new CharacterInfoProvenance();
                characterInfoProvenance.Content = dr["CONTENT"].ToString();
                characterInfoProvenance.AurthName = dr["AURTH_NAME"].ToString();
                characterInfoProvenance.ArticelName = dr["ARRTICLE_NAME"].ToString();
                characterInfoProvenance.CharacterInfo = characterInfo;
                listCharacterInfoProvenance.Add(characterInfoProvenance);
            }
            dr.Close();
            return listCharacterInfoProvenance;
        }


        public bool DeleteCharacterInfoProvenance(CharacterInfo characterInfo)
        {
            string sql = "delete from bn_character_info_provenance where character_info_id = " + GetCharacterInfoId(characterInfo.Meaning, characterInfo.SoundMark, characterInfo.PartOfSpeech).ToString();
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
            return true;

        }
    }


}
